package cn.avalon.org.service.impl;

import cn.avalon.basic.service.impl.BaseServiceImpl;
import cn.avalon.basic.utils.Md5Utils;
import cn.avalon.basic.utils.StrUtils;
import cn.avalon.org.domain.Employee;
import cn.avalon.org.mapper.EmployeeMapper;
import cn.avalon.org.service.EmployeeService;
import cn.avalon.user.domain.Logininfo;
import cn.avalon.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void save(Employee employee) {
        // 处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
        // 如果员工存在角色，就保存到角色中间表
        if (employee.getRoleId() != null) {
            employeeMapper.saveEmployeeRole(employee);
        }
    }

    @Override
    public void remove(Long id) {
        // 删除登录信息logininfo
        Employee employee = employeeMapper.findById(id);
        if (employee != null) {
            logininfoMapper.remove(employee.getLogininfo_id());
        }
        // 先删除员工中间表再删除员工表
        employeeMapper.removeEmployeeRole(id);
        logininfoMapper.remove(employee.getLogininfo_id());

        // 删除employee信息
        super.remove(id);
    }

    @Override
    public void update(Employee employee) {
        // 先删除员工角色中间表，在重新添加
        employeeMapper.removeEmployeeRole(employee.getId());
        if (employee.getRoleId() != null) {
            employeeMapper.saveEmployeeRole(employee);
        }

        // 处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        super.update(employee);

        // 获取登录信息id
        Long logininfo_id = employeeMapper.findById(employee.getId()).getLogininfo_id();
        // 创建logininfo
        Logininfo logininfo = logininfoMapper.findById(logininfo_id);// id,type ,state
        // 将新的数据覆盖旧的数据
        BeanUtils.copyProperties(employee,logininfo);//5个数据新的 - 用户名，邮箱，电话，盐值，密码
        // 重置logininfo_id
        logininfo.setId(logininfo_id);
        logininfoMapper.update(logininfo);
    }

    @Override
    public List<Employee> findShopEmployee(Long shopId) {
        return employeeMapper.findShopEmployee(shopId);
    }
}
